<?php
class loginControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		
		
	}
	
	public function onDefault(){
		if(SHOPID){
			$this->gourl(APPKUCUN."?m=index");
		}
		$this->smarty->display("login/login.html");
	}
	
	public function onSave(){
		$adminname=post('adminname','h');
		$row=M("kucun_admin")->selectRow(array("where"=>" adminname='".$adminname."' "));
		if($row && $row['password']==umd5(post('password').$row['salt'])){
			$_SESSION['sskucunadmin']=$row;
			$authcode=jiami($row['adminid']."|".umd5($row['password']));
			setcookie("kc_authcode",$authcode,time()+3600000,"/",DOMAIN);
			//处理推送相关
			M("apppush")->add("kucunadmin",$row);
			
			$this->goall("登录成功",0,0,APPKDDIAN."?m=iframe");
		}else{
			$this->goall("账户或者密码出错",1);
		}
	}
	
	public function onlogout(){
		$_SESSION['sskucunadmin']="";
		 setcookie("kc_authcode","",time()-3999,"/",DOMAIN);	
		$this->goall("退出成功",0,0,APPKUCUN."/index.php");
	}
	
	 
	
	public function onAdmin(){
		M("login")->checkAdminLogin();
		$kcid=get('kcid','i');
		$kucun=M("kucun")->selectRow("kcid=".$kcid);
		if(empty($kucun)){
			$this->goAll("库存不存在");
		}
		$kucun_admin=M("kucun_admin")->selectRow(array("where"=>"shopid=".$kcid." "));
	 
		if(empty($kucun_admin)){
				$adminid=M("kucun_admin")->insert(array(
					"salt"=>1234,
					"shopid"=>$kucun['kcid'],
					"adminname"=>$kucun['kcid']."@admin.com",
					"password"=>umd5(time()),
					 
				));
				$kucun_admin=M("kucun_admin")->selectRow(array("where"=>"adminid=".$adminid." "));	
		}
		
		 
		$_SESSION['sskucunadmin']=$kucun_admin;
		
		 
		$this->gourl(APPKUCUN."?m=iframe"); 
	}
	
	public function onKouDai(){
		$kid=intval($_SESSION['sskoudaiadmin']['shopid']);
		if($_SESSION['sskoudaiadmin']['typeid']!=1) $this->goAll("您无权限",1);
		$koudai=M("koudai")->selectRow("id=".$kid);
		if(empty($koudai)){
			$this->goAll("您无权限",1);
		}
		$kucun=M("kucun")->selectRow("koudai=".$kid);
		 
		if($kucun){
			$kucun_admin=M("kucun_admin")->selectRow(array("where"=>"shopid=".$kucun['kcid']." "));		
			if(empty($kucun_admin)){
				M("kucun_admin")->insert(array(
					"salt"=>1234,
					"shopid"=>$kucun['kcid'],
					"adminname"=>$kucun['kcid']."@admin.com",
					"password"=>umd5(time()),
					
				));
				$kucun_admin=M("kucun_admin")->selectRow(array("where"=>"shopid=".$kucun['kcid']." "));	
			}
			$_SESSION['sskucunadmin']=$kucun_admin;
			if(get('to','h')){
				$this->goUrl(APPKUCUN."?m=".get('to','h'));
			}else{
				$this->gourl(APPKUCUN."?m=iframe");
			}
		}else{
			$this->goAll("您无权限",1);
		}
		
	}
		
}
?>